#include "EnergyBar.h"
#include "Resources.h"
#include "RenderManager.h"

EnergyBar::EnergyBar(const std::string& id, const sf::Vector2f& position)
{
	_position = position;
	_maxPower = 56;
	_currentPower = 20;
	Resources::getSingletonPtr()->loadSprite(_top, "BAR_TOP");
	Resources::getSingletonPtr()->loadSprite(_middle, "BAR_MIDDLE");
	Resources::getSingletonPtr()->loadSprite(_energyBar, "BAR_FILL");
	Resources::getSingletonPtr()->loadSprite(_bot, id);
	_top.setPosition(_position);
	_middle.setTextureRect(sf::IntRect(_middle.getTextureRect().left, _middle.getTextureRect().top, _middle.getTextureRect().width, _middle.getTextureRect().height * _maxPower));
	_middle.setPosition(_position.x, _position.y + _top.getTextureRect().height + (_middle.getTextureRect().height));
	_middle.setOrigin(sf::Vector2f(static_cast<float>(_middle.getTextureRect().left), static_cast<float>(_middle.getTextureRect().top + _middle.getTextureRect().height)));

	_energyBar.setTextureRect(sf::IntRect(_energyBar.getTextureRect().left, _energyBar.getTextureRect().top, _energyBar.getTextureRect().width, _energyBar.getTextureRect().height * _currentPower));
	_energyBar.setPosition(_position.x, _position.y + _top.getTextureRect().height + (_middle.getTextureRect().height));
	_energyBar.setOrigin(sf::Vector2f(static_cast<float>(_energyBar.getTextureRect().left), static_cast<float>(_energyBar.getTextureRect().top + _energyBar.getTextureRect().height)));
	_bot.setPosition(_position.x, _position.y + (_top.getTextureRect().height + static_cast<float>(_maxPower * 2)));
}


EnergyBar::~EnergyBar(void)
{
}

void EnergyBar::update()
{

}

void EnergyBar::draw(RenderManager* rm)
{
	rm->draw(_top);
	rm->draw(_middle);
	rm->draw(_energyBar);
	rm->draw(_bot);
}

void EnergyBar::setMaxPower(int maxPower)
{
	_maxPower = maxPower;
	_middle.setTextureRect(sf::IntRect(_middle.getTextureRect().left, _middle.getTextureRect().top, _middle.getTextureRect().width, _middle.getTextureRect().height * _maxPower));
	_middle.setPosition(_position.x, _position.y + _top.getTextureRect().height + (_middle.getTextureRect().height));
	_middle.setOrigin(sf::Vector2f(static_cast<float>(_middle.getTextureRect().left), static_cast<float>(_middle.getTextureRect().top + _middle.getTextureRect().height)));

	_energyBar.setTextureRect(sf::IntRect(_energyBar.getTextureRect().left, _energyBar.getTextureRect().top, _energyBar.getTextureRect().width, _energyBar.getTextureRect().height * _currentPower));
	_energyBar.setPosition(_position.x, _position.y + _top.getTextureRect().height + (_middle.getTextureRect().height));
	_energyBar.setOrigin(sf::Vector2f(static_cast<float>(_energyBar.getTextureRect().left), static_cast<float>(_energyBar.getTextureRect().top + _energyBar.getTextureRect().height)));
	_bot.setPosition(_position.x, _position.y + (_top.getTextureRect().height + static_cast<float>(_maxPower * 2)));
}

void EnergyBar::setPower(int power)
{
	_currentPower = power;
	if (_currentPower > _maxPower)
		_currentPower = _maxPower;
	_energyBar.setTextureRect(sf::IntRect(_energyBar.getTextureRect().left, _energyBar.getTextureRect().top, _energyBar.getTextureRect().width, _energyBar.getTextureRect().height * _currentPower));
	_energyBar.setPosition(_position.x, _position.y + _top.getTextureRect().height + (_middle.getTextureRect().height));
	_energyBar.setOrigin(sf::Vector2f(static_cast<float>(_energyBar.getTextureRect().left), static_cast<float>(_energyBar.getTextureRect().top + _energyBar.getTextureRect().height)));
}